<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Mixer extends Controller {

	private static $fn = 100;
	private static $delim = '-.-';

	public function after() {
		parent::after();
	}

	public function action_index() {
		$errors = NULL;
		set_time_limit(600);
		if (Request::current()->method() == Request::POST) {
			$files = $_FILES['docs'];
			array_multisort($files['name'], $files['type'], $files['tmp_name'], $files['error'], $files['size']);
			unset($_FILES);
			$ilosc = count($files['name']);
			for ($i = 0; $i < $ilosc; $i++) {
				$_FILES['file' . $i]['name'] = $files['name'][$i];
				$_FILES['file' . $i]['type'] = $files['type'][$i];
				$_FILES['file' . $i]['tmp_name'] = $files['tmp_name'][$i];
				$_FILES['file' . $i]['error'] = $files['error'][$i];
				$_FILES['file' . $i]['size'] = $files['size'][$i];
			}
			$validate = Validation::factory($_FILES);
			for ($i = 0; $i < $ilosc; $i++) {
				$validate->rule('file' . $i, 'Upload::size', array(':value', '5000KiB'));
				$validate->rule('file' . $i, 'Upload::type', array(':value', array('txt')));
				$validate->rule('file' . $i, 'Upload::valid');
			}

			if ($validate->check()) {
				if (!empty($ilosc)) {
					foreach ($_FILES as $i => &$file) {
						if ($filepath = Upload::save($file, 'docs' . $i . '.txt', 'files/mixer')) {
							//$data[$i] = iconv('utf-8', 'cp1251//IGNORE', file_get_contents($filepath));
							$data[$i] = file_get_contents($filepath);
							unlink($filepath);
						}
					}
				}
			}
			else {
				$errors = $validate->errors('mixer');
			}
			if (!empty($data)) {
				foreach ($data as $id => $file) {
					unset($data[$id]);
					$data[$id] = explode(self::$delim, $file);
				}
				$cnt = count(reset($data));
				for ($i = 0; $i < $cnt; $i++) {
					foreach ($data as $id => &$file) {
						$new[] = $file[$i];
					}
				}
			}
		}

		if (!empty($new)) {
			//$this->response->headers('Content-Type','text/html; charset=cp1251');
			$this->response->body(implode(self::$delim, $new));
			$this->response->send_file(TRUE, 'mixer_' . date('Y.m.d_H-i-s') . '.txt');
		}
		else {
			$this->response->body(View::factory($this->request->controller(), array('files' => self::$fn, 'errors' => $errors)));
		}
	}

	public function action_info() {
		phpinfo();
	}

	public function action_check() {
		$url = 'http://ya.ru';
		$urls = explode("\n", Arr::get($_POST, 'urls', array($url)));
		$checked = '';
		foreach ($urls as $url) {
			$url = trim($url);
			if (!empty($url)) {
				$request = Request::factory($url)
						->method(Request::GET);

				$request->client()
						->options(array(
							CURLOPT_SSL_VERIFYPEER => FALSE,
							CURLOPT_USERAGENT => Request::$user_agent,
							CURLOPT_HTTPHEADER => array(
								//'Accept-Language:ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3',
								'Accept-Encoding:gzip,deflate,sdch',
								'Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
							),
							CURLOPT_FOLLOWLOCATION => TRUE,
				));
				$response = $request->execute();
				$data = $response->body();
				//exit($url);
				$checked .= '<div class="itemcheck">'.HTML::anchor($url, $url);
				if (preg_match('/<div class="item-subtitle">(.*?)</si',$data,$m)){
					$checked .= '||'.$m[1];
				}
				if (preg_match('/<h1 itemprop="name" class="h1">(.*?)<\/h1>/si',$data,$m)){
					$checked .= '||'.$m[1];
				}
				if (preg_match('/<div class="item-views">(.*?)<\/div>/si',$data,$m)) {
					$checked .= '||<b>'.$m[1].'</b><br>'; 
				}
				elseif (preg_match('/<div class="item-views">(.*?)<\/div>/si',$data,$m)){
					
				}
				else {
					$checked .=  '||<b>не найдено</b><br>';
				}
				$checked .= '</div>';
				//echo htmlspecialchars($data);
			}
		}
		$this->response->body(View::factory($this->request->controller().'_'.$this->request->action(), array('checked' => $checked, 'urls' => implode("\n",$urls))));
	}

}

// End Parse
